<?php
namespace app\api\controller;

class User extends AuthBase {
    public function index() {
        $user = (new \app\common\business\User())->getNormalUserById($this->userId);
        $resultUser = [
            'id'=>$this->userId,
            'username'=>$user['username'],
            'sex'=>$user['sex']
        ];
        return show(config('status.success'), 'OK', $resultUser);
    }

    public function update() {
        $username = input('param.username','','trim');
        $sex = input('param.sex',0,'intval');
        $data = [
            'username'=>$username,
            'sex'=>$sex
        ];
        $validate = (new \app\api\validate\User())->scene('update_user');
        if ( ! $validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }
        $userBisObj = new \app\common\business\User();
        $user = $userBisObj->update($this->userId, $data);
        if (empty($user)){
            return show(config('status.error'), '更新失败');
        }
        // 如果用户名被修改,redis里面的数据也需要同步

        return show(config('status.success'), 'OK');
    }
}